package cn.jbolt.admin.user;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.JBoltApiKit;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.cache.JBoltGlobalConfigCache;
import cn.jbolt.core.cache.JBoltUserCache;
import cn.jbolt.core.cache.JBoltUserExtendCache;
import cn.jbolt.core.kit.JBoltUserKit;
import cn.jbolt.core.model.User;
import cn.jbolt.core.model.UserExtend;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltRoleService;
import cn.jbolt.core.service.JBoltUserExtendService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.tx.Tx;
@CheckPermission(PermissionKey.USER)
@UnCheckIfSystemAdmin
@Path("/api/admin/user")
public class UserAdminController extends JBoltApiBaseController {
	@Inject
	private UserService service;
	@Inject
	private JBoltUserExtendService userExtendService;
	@Inject
	private JBoltRoleService roleService;

	/**
	 * 获取单个用户信息
	 */
	public void index(){
		Long userId=getId();
		if(notOk(userId)){
			renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
			return;
		}
		User user=service.findById(userId);
		if(user==null) {
			renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
			return;
		}
		User me=service.findById(JBoltUserKit.getUserId());
		if((user.getIsSystemAdmin()!=null&&user.getIsSystemAdmin())&&(me.getIsSystemAdmin()==null|| !me.getIsSystemAdmin())) {
			renderJBoltApiFail("无权修改超管员信息");
			return;
		}
		Kv data = Kv.by("user",user);
		boolean userExtendEnable = JBoltGlobalConfigCache.me.userExtendEnable();
		if(userExtendEnable){
			data.set("extend", JBoltUserExtendCache.me.get(userId));
		}
		renderJBoltApiSuccessWithData(data);
	}

	@UnCheck
	public void mineInfo(){
		Long userId= JBoltApiKit.getApiUserIdToLong();
		if(notOk(userId)){
			renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
			return;
		}
		User user=service.findById(userId);
		if(user==null) {
			renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
			return;
		}
		User me=service.findById(JBoltUserKit.getUserId());
		if((user.getIsSystemAdmin()!=null&&user.getIsSystemAdmin())&&(me.getIsSystemAdmin()==null|| !me.getIsSystemAdmin())) {
			renderJBoltApiFail("无权修改超管员信息");
			return;
		}
		Kv data = Kv.by("user",user);
		boolean userExtendEnable = JBoltGlobalConfigCache.me.userExtendEnable();
		if(userExtendEnable){
			data.set("extend", JBoltUserExtendCache.me.get(userId));
		}
		renderJBoltApiSuccessWithData(data);
	}

	/**
	 * 表格数据接口
	 */
	public void datas(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
					  @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
					  @Para(KEYWORDS_PARAM_NAME)String keywords,
					  @Para(OFMODULE_PARAM_NAME)Integer ofModule,
					  @Para("sex")Integer sex,
					  @Para("deptId")Long deptId,
					  @Para("postId")Long postId,
					  @Para("roleId")Long roleId,
					  @Para(ENABLE_PARAM_NAME)Boolean enable) {
		renderJBoltApiSuccessWithData(service.paginateAdminList(page,pageSize,keywords,ofModule,sex,deptId,postId,roleId,enable));
	}

	/**
	 *  系统通知 可用 选择用户数据接口
	 */
	@CheckPermission(PermissionKey.SYS_NOTICE)
	public void sysnoticeUsers(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
							   @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
							   @Para(KEYWORDS_PARAM_NAME)String keywords,
							   @Para("sex")Integer sex,
							   @Para("deptId")Long deptId,
							   @Para("postId")Long postId,
							   @Para("roleId")Long roleId) {
		renderJBoltApiSuccessWithData(service.paginateSysNoticeList(page,pageSize,keywords,sex,deptId,postId,roleId));
	}

	/**
	 * 重置用户密码
	 */
	@Before(Tx.class)
	public void resetPwd(){
		Long userId=getLong("userId");
		String newPwd=get("newPwd");
		String reNewPwd=get("reNewPwd");
		if(notOk(newPwd)||notOk(reNewPwd)){
			renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
			return;
		}
		if(!newPwd.equals(reNewPwd)){
			renderJBoltApiFail("两次新密码输入不一致");
			return;
		}
		renderJBoltApiRet(service.resetPwd(userId,newPwd));
	}

	/**
	 * 修改用户自己的密码
	 */
	@UnCheck
	@Before(Tx.class)
	public void updatePwd(){
		String originPwd=get("originPwd");
		String newPwd=get("newPwd");
		String reNewPwd=get("reNewPwd");
		if(notOk(originPwd)||notOk(newPwd)||notOk(reNewPwd)){
			renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
			return;
		}
		if(!newPwd.equals(reNewPwd)){
			renderJBoltApiFail("两次新密码输入不一致");
			return;
		}
		renderJBoltApiRet(service.updatePwd(originPwd,newPwd));
	}

	/**
	 * 保存
	 */
	@Before(Tx.class)
	@JBoltHttpPost
	public void save(@Para("user")User user,@Para("extend")UserExtend extend){
		renderJBoltApiRet(service.save(user,extend));
	}
	/**
	 * 更新
	 */
	@Before(Tx.class)
	@JBoltHttpPost
	public void update(@Para("user")User user,@Para("extend")UserExtend extend){
		renderJBoltApiRet(service.update(user,extend));
	}
	/**
	 * 更新个人信息
	 */
	@Before(Tx.class)
	@JBoltHttpPost
	@UnCheck
	public void updateMineInfo(@Para("")User user){
		User updateUser = new User();
		updateUser.setId(JBoltApiKit.getApiUserIdToLong());
		updateUser.setName(user.getName());
		updateUser.setAvatar(user.getAvatar());
		updateUser.setSex(user.getSex());
		updateUser.setAge(user.getAge());
		updateUser.setEmail(user.getEmail());
		updateUser.setPhone(user.getPhone());
		updateUser.setUsername(JBoltUserCache.me.getUserName(updateUser.getId()));

		renderJBoltApiRet(service.update(updateUser, null));
	}




	/**
	 * 删除
	 */
	@Before(Tx.class)
	@JBoltHttpGet
	public void delete(){
		renderJBoltApiRet(service.delete(getLong(0)));
	}
	/**
	 * 切换启用状态
	 */
	@Before(Tx.class)
	@JBoltHttpGet
	public void toggleEnable(){
		renderJBoltApiRet(service.toggleEnable(getLong(0)));
	}
	@Override
	protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
		return service;
	}
}
